package com.changgou.search.controller;


import com.changgou.entity.Page;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/search")
public class SearchController {


    @Autowired
    private SearchService searchService;


    @GetMapping("/list")
    public String list(@RequestParam Map<String, String> searchMap, Model model) {

        //特殊符号的处理
        this.handleSearchMap(searchMap);

        //获取查询结果
        Map resultMap = searchService.search(searchMap);


        //携带数据跳转页面
        model.addAttribute("result", resultMap);

        //携带searchMap数据跳转页面
        model.addAttribute("searchMap", searchMap);

        //封装分页数据返回
        //1.总记录数
        //2.当前页
        //3.每页显示多少条
        Page<SkuInfo> page = new Page<SkuInfo>(

                //总记录数
                Long.parseLong(String.valueOf(resultMap.get("total"))),
                //当前页
                Integer.parseInt(String.valueOf(resultMap.get("pageNum"))),
                //每页显示的条数
                Page.pageSize
        );
        model.addAttribute("page",page);





        //拼装url参数
        ///search/list  基础查询路径
        StringBuilder url = new StringBuilder("/search/list");
        if (searchMap != null && searchMap.size() > 0) {
            //是有查询条件的
            url.append("?");

            //paramKay  代表了搜索名称
            for (String paramKey : searchMap.keySet()) {
                //sortRule 当前排序的规则
                if (!"sortRule".equals(paramKey) && !"sortField".equals(paramKey) && !"pageNum".equals(paramKey)) {

                    url.append(paramKey).append("=").append(searchMap.get(paramKey)).append("&");

                }

            }
            //http://localhost:9009/search/list?keywords=手机&spec_网络制式=4G
            String  urlString = url.toString();

            //切割路径上的最后一个&号
            urlString = urlString.substring(0,urlString.length()-1);

            //返回数据模型
            model.addAttribute("url",urlString);
        }else {
            model.addAttribute("url",url);
        }

        return "search";
    }


    @GetMapping
    @ResponseBody
    public Map search(@RequestParam Map<String, String> searchMap) {

        //特殊符号的处理
        this.handleSearchMap(searchMap);

        Map searchResult = searchService.search(searchMap);
        /**
         * 包含了 查询的总页数 ，总记录数 等等
         */
        return searchResult;
    }

    private void handleSearchMap(Map<String, String> searchMap) {
        Set<Map.Entry<String, String>> entries = searchMap.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            if (entry.getKey().startsWith("spec_")) {
                searchMap.put(entry.getKey(), entry.getValue().replace("+", "%2B"));

            }
        }
    }
}
